package com.xiaoxiao.homework;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class XiaoHomework {

    private static String name;
    private static BigDecimal height;
    private static BigDecimal weight;
    private static String gender;

    public static void main(String[] args) {
        //1.记录输入的数据到 name height weight gender
        Scanner scanner = new Scanner(System.in);
        String[] paramValues = inputBox(scanner);
        List<String> wrongs = validateIsWrong(paramValues);

        //2.校验输入的数据是否正确
        while (wrongs.size() != 0) {
            wrongs.forEach(wrong -> System.out.print(wrong + " "));
            System.out.println("输入有误，请重新输入");
            wrongs = validateIsWrong(inputBox(scanner));
        }
        scanner.close();

        //3.获取输入值
        name = paramValues[0];
        height = new BigDecimal(paramValues[1]);
        weight = new BigDecimal(paramValues[2]);
        gender = paramValues[3].equals("F") ? "女士" : "男士";

        //4.计算BIM值
        Double bim = weight.divide(height.pow(2), 3, BigDecimal.ROUND_HALF_UP).doubleValue();
        String result = calculateBodySize(bim);
        System.out.println("您的身材" + result);
        System.out.println("感谢" + name + gender + "使用本程序，祝您身体健康！");
    }


    /**
     * 输入框
     */
    private static String[] inputBox(Scanner scanner) {
        String[] paramKeys = new String[]{"姓名", "身高(单位/米)", "体重(单位/公斤)", "性别(M或F)"};
        String[] paramValues = new String[4];
        for (int i = 0; i < 4; i++) {
            System.out.println("请输入" + paramKeys[i] + ":");
            paramValues[i] = scanner.next();
        }
        return paramValues;
    }


    /**
     * 校验非法的输入项
     */
    private static List<String> validateIsWrong(String[] paramValues) {
        ArrayList<String> wrongs = new ArrayList<>();
        if (paramValues[1].matches("\\d+(\\.\\d+)?")) wrongs.add("身高");
        if (paramValues[2].matches("\\d+(\\.\\d+)?")) wrongs.add("体重");
        if (!paramValues[3].equals("M") && !paramValues[3].equals("F")) wrongs.add("性别");
        return wrongs;
    }


    /**
     * 根据BIM值生成健康结果
     */
    private static String calculateBodySize(Double bim) {
        if (bim <= 18.4) return "偏瘦";
        if (bim <= 23.9) return "正常";
        if (bim < 28.0) return "偏胖";
        return "肥胖";
    }

}
